import { prisma } from "@/lib/prisma"
import { ReportsHeader } from "@/components/reports/reports-header"
import { ReportsList } from "@/components/reports/reports-list"
import { Metadata } from "next"
import { SerializedFinanceRecord } from "@/types"

export const metadata: Metadata = {
  title: "财务报表 - 商场销售系统",
  description: "财务报表页面",
}

export default async function ReportsPage() {
  const incomeRecords = await prisma.financeRecord.findMany({
    where: {
      type: 'INCOME'
    },
    orderBy: {
      createdAt: 'desc'
    }
  })

  const expenseRecords = await prisma.financeRecord.findMany({
    where: {
      type: 'EXPENSE'
    },
    orderBy: {
      createdAt: 'desc'
    }
  })

  // 序列化数据
  const serializedIncomeRecords = incomeRecords.map(record => ({
    ...record,
    amount: record.amount.toString(),
    createdAt: record.createdAt.toISOString(),
    updatedAt: record.updatedAt.toISOString()
  }))

  const serializedExpenseRecords = expenseRecords.map(record => ({
    ...record,
    amount: record.amount.toString(),
    createdAt: record.createdAt.toISOString(),
    updatedAt: record.updatedAt.toISOString()
  }))

  return (
    <div className="space-y-4 p-8">
      <ReportsHeader incomeRecords={serializedIncomeRecords} expenseRecords={serializedExpenseRecords} />
      <ReportsList incomeRecords={serializedIncomeRecords} expenseRecords={serializedExpenseRecords} />
    </div>
  )
} 